

#include "disp_manager.h"
#include <linux/kernel.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <input_manager.h>

#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
#include <string.h>



//管理触摸屏和UDP



static inputDevice_p g_inppdevs = NULL;



/*start of 环形缓冲区*/
#define BUFFER_LEN 20
static int g_read =0;
static int g_write =0;
static inputEvent g_inputevents[BUFFER_LEN];
static int InputBuffFull(void )
{


	if(g_read ==((g_write + 1)%BUFFER_LEN))
	{
		return 1;

	}
	else
	{

		return 0;
	}


}
static int InputBuffEmpty(void)
{

		if(g_read == g_write)
	{
		return 1;

	}
	else
	{

		return 0;
	}



}

static void  PutInputEventToBuff(inputEvent_p inputeventp)
{
	if(!InputBuffFull())
	{

		g_inputevents[g_write] = *inputeventp;
		g_write= (g_write+1)%BUFFER_LEN;


	}
}

static int   GetInputEventFromBuff(inputEvent_p inputeventp)
{
	if(!InputBuffEmpty())
	{

		*inputeventp  = g_inputevents[g_read]  ;
		g_read= (g_read+1)%BUFFER_LEN;
		return 1;

	}
	else
	{
		return 0;



	}
}
/*end of 环形缓冲区*/








static pthread_mutex_t g_tMutex =PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t g_tConVar =PTHREAD_COND_INITIALIZER;
static void *input_recv_thread_func(void *data)
{
	inputDevice_p dev =(inputDevice_p) data;
	inputEvent   g_tInputBuff ;
	int ret ;


	
	while(1)
	{
		ret  = dev->GetInputEvent(&g_tInputBuff);
		if(!ret)
		{
			//保存数据，
			pthread_mutex_lock(&g_tMutex);
			PutInputEventToBuff(&g_tInputBuff);
			pthread_cond_signal(&g_tConVar);//唤醒其他等待函数的线程
			pthread_mutex_unlock(&g_tMutex);	
			//唤醒等待数据的线程
		}

	}
	return NULL;



}




void RegsiterInput(inputDevice_p inputdevice_p)
{

	inputdevice_p->ptNEXT = g_inppdevs;

	g_inppdevs = inputdevice_p;



}


int initdeviceInput(void)
{
	int ret;
	pthread_t t_ID;
	
	inputDevice_p ptmp = g_inppdevs;
	while(ptmp)
	{

		ret =ptmp->DeviceInit();
		//ret = g_inppdevs.GetInputEvent(&g_tInputBuff);
		if(!ret)
		{
			
			//初始化成功，创建线程pthread_create
			ret = pthread_create(&t_ID, NULL, input_recv_thread_func, ptmp);
		}
		ptmp = ptmp->ptNEXT;
	}
	return 0;

}






//初始化系统
int Inputinit(void)
{


	extern void SocketNetRegsiter(void);
	extern void TouchScreenRegsiter(void);

	
	SocketNetRegsiter();
	TouchScreenRegsiter();
	return 0;
}



int GetALLInputEvent(inputEvent_p inputEventp)
{
	 
	int ret ;
	inputEvent getinputEvent;


	pthread_mutex_lock(&g_tMutex);
	if(GetInputEventFromBuff(&getinputEvent))
	{
		*inputEventp = getinputEvent;
		pthread_mutex_unlock(&g_tMutex);
		
		printf("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
		return 0;

	}
	else
	{
		//休眠等待
		pthread_cond_wait(&g_tConVar, &g_tMutex);
		if(GetInputEventFromBuff(&getinputEvent))
		{	
			*inputEventp = getinputEvent;
			ret = 0;
		}
		else
		{
			ret = -1;
		}
		pthread_mutex_unlock(&g_tMutex);

		
		printf("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);

	}
	return ret;
}
















